/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.braincasedesigns.roller.model;

import com.braincasedesigns.roller.model.DieModifier;

/**
 *
 * @author jcampbel
 */
public class StandardModifier implements DieModifier<PolyhedralFace>{
    private final String operator;
    private final int operand;
    private final Op op;

    public StandardModifier(String operator, int operand){
        this.operator = operator;
        this.operand = operand;
        op = Op.getOperator(operator);
        if (op == null){
            throw new IllegalArgumentException("Unrecognized operation:"+operator);
        }
    }
    
    public int getOperand() {
        return operand;
    }

    public String getOperator() {
        return operator;
    }

    public int apply(PolyhedralFace face) {
        return apply(face.getNumericRepresentation());
    }
    public int apply(int input){
        return op.operate(input, operand);
        
    }
    
    public String getNotation(){
        return operator+operand;
    }
    
    public enum Op{
        ADD("+"){
            public int operate(int input, int operand){
                return input + operand;
            }
        },
        SUBTRACT("-"){
            public int operate(int input, int operand){
                return input - operand;
            }
        },
        DIVIDE("/"){
            public int operate(int input, int operand){
                return input / operand;
            }
        },
        MULTIPLY("*"){
            public int operate(int input, int operand){
                return input * operand;
            }
        };
        private final String symbol;
       
        private Op(String symbol){
            this.symbol = symbol;
        }
        
        public boolean matches(String s){
            return symbol.equals(s);
        }
        
        public abstract int operate(int input, int operand);
        
        public static Op getOperator(String symbol){
            Op[] ops = Op.values();
            Op match = null;
            for (int i = 0; i < ops.length; i++){
                Op op = ops[i];
                if (op.matches(symbol)){
                    match = op;
                    break;
                }
            }
            return match;
        }
        
    }
}
